﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Deployment.Application;

namespace ParsecClient.Classes
{
    public static class Serializer
    {
        public static T Deserialize<T>(this string source)
        {
            return source.Deserialize<T>(false);
        }
        public static T Deserialize<T>(this string source, bool suppressFailureException)
        {
            using (MemoryStream memoryStream = new MemoryStream(System.Convert.FromBase64String(source)))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                try
                {
                    return (T)binaryFormatter.Deserialize(memoryStream);
                }
                catch
                {
                    if (suppressFailureException)
                        return default(T);
                    else
                        throw;
                }
            }
        }
        public static string Serialize<T>(this T source)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(memoryStream, source);
                return System.Convert.ToBase64String(memoryStream.ToArray());
            }
        }
        public static string GetFullPath(string filename)
        {

            string folder;
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                folder = ApplicationDeployment.CurrentDeployment.DataDirectory;
            }
            else
            {
                folder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Parsec");
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
            }
            return Path.Combine(folder, filename);
        }
        public static void SerializeDataToDisk(System.Xml.Serialization.XmlSerializer s, object csc, string fullPath)
        {
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fullPath))
            {
                s.Serialize(writer, csc);
                //writer.Close();
            }
        }
        public static object DeserializeDataFromDisk(System.Xml.Serialization.XmlSerializer s, string fullPath, bool suppressFailureException)
        {
            using (System.IO.StreamReader reader = new System.IO.StreamReader(fullPath))
            {
                try
                {
                    return s.Deserialize(reader);
                }
                catch
                {
                    if (suppressFailureException)
                        return null;
                    else
                        throw;
                }
            }
        }
    }
}
